<?php

/**
 * 后台权限验证模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: check.class.php 31 2011-07-11 15:25:51Z zwt007 $
 * http://www.t-mac.org；
 */
class check extends Model
{

    private $users_table;
    private $users_type_table;
    private $userslog_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->users_table = '#@__users';
        $this->userstype_table = '#@__users_type';
        $this->userslog_table = '#@__users_log';
    }

    /**
     * 检查是否登录
     * @param string $info
     * return Boole
     */
    public function checkLogin()
    {
        if (empty($_SESSION['admin'])) {
            if (isset($_COOKIE['cookie_remember']) && $_COOKIE['cookie_remember'] == "yes") { //如果存在cookie 如果cookie_remember是正确的
                $username = $_COOKIE['cookie_username'];
                $password = $_COOKIE['cookie_password'];
                $sql = "SELECT a.type_purviews, b.* FROM $this->userstype_table a LEFT JOIN $this->users_table b ON a.rank = b.rank WHERE b.username = '{$username}' LIMIT 0, 1";
                $info = $this->db->getRow($sql);
                if ($info) {
                    if (md5(md5($password)) == $info['password']) { //因客户需要 取消 密码的MD5加密
                        if ($_SESSION['admin'] != '') {
                            session_unregister('admin');
                        }
                        session_register('admin');
                        $_SESSION['admin'] = $username;
                        $_SESSION["admin_uid"] = $info['uid'];
                        $_SESSION['admin_purviews'] = $info['type_purviews'];

                        $uid = $info['uid'];
                        $time = time();
                        $logip = $_SERVER["REMOTE_ADDR"];
                        //插adminlog 登录日志
                        $log_info = array(
                            'rank' => $info['rank'],
                            'uid' => $uid,
                            'username' => $info['username'],
                            'logip' => $logip,
                            'logtime' => $time
                        );
                        $rs = $this->db->autoExecute($this->userslog_table, $log_info, 'INSERT');
                        //更新用户上次登录时间，IP，登录次数
                        $sql = "UPDATE $this->users_table SET login_ip='$logip', login_time='$time', logincount=logincount+1 WHERE uid = $uid";
                        $rs = $this->db->execute($sql);
                        if ($rs) {
                            $this->redirect("成功登录，正在转向后台！", PHP_SELF . "?m=admin");
                            exit();
                        }
                    } else {
                        $this->redirect("Access Denied! 密码错误", PHP_SELF . "?m=login");
                        exit();
                    }
                } else {
                    $this->redirect("Access Denied! 没有此用户", PHP_SELF . "?m=login");
                    exit();
                }
            }
            $this->redirect("Access Denied! 请您登录", PHP_SELF . "?m=login");
            exit;
        }
    }

    /**
     * 取得当前用户所有的权限组
     * @return <type>
     */
    public function getPurview()
    {
        $purview = isset($_SESSION['admin_purviews']) ? $_SESSION['admin_purviews'] : '';
        return $purview;
    }

    /**
     * 检验用户是否有权使用某功能
     * @param <type> $n
     * @return boolean
     */
    public function TestPurview($n)
    {
        $rs = false;
        $purview = $this->getPurview();        
        if ($n == '') {
            return true;
        }
        if (!isset($GLOBALS['groupRanks'])) {
            $GLOBALS['groupRanks'] = explode(' ', $purview);
        }
        $ns = explode(',', $n);
        foreach ($ns as $n) {
            //只要找到一个匹配的权限，即可认为用户有权访问此页面
            if ($n == '') {
                continue;
            }
            if (in_array($n, $GLOBALS['groupRanks'])) {
                $rs = true;
                break;
            }
        }
        return $rs;
    }

    /**
     * 页面上面的check
     * @param <type> $n
     */
    public function CheckPurview($n)
    {        
        if (!$this->TestPurview($n)) {
            $this->redirect("对不起，你没有权限执行此操作！返回上一页");
            exit();
        }
    }

    /**
     * menu上面的check
     * @param <type> $n
     * @return boolean
     */
    public function CheckPurviewMenu($n)
    {
        if (!$this->TestPurview($n)) {
            $a = true;
        } else {
            $a = false;
        }
        return $a;
    }

}